package com.example.blog.service.impl;

import com.example.blog.dao.tagRepository;
import com.example.blog.dao.typeRepository;
import com.example.blog.pojo.tag;
import com.example.blog.pojo.type;
import com.example.blog.service.tagService;
import com.example.blog.service.typeService;
import com.github.pagehelper.Page;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class tagServiceImpl implements tagService {

    @Autowired
    tagRepository tagRepository;

    @Transactional
    @Override
    public void saveTag(tag tag) {
        tagRepository.save(tag);
    }

    @Override
    public tag getTag(Integer id) {
        return tagRepository.findById(id);
    }

    @Override
    public tag getTag(String name) {
        return tagRepository.findByName(name);
    }

    @Override
    public Page<tag> listTag() {
        return tagRepository.findAll();
    }

    @Override
    public Page<tag> list() {
        return tagRepository.find();
    }

    @Transactional
    @Override
    public void updateTag(Integer id, String name) throws NotFoundException {
        tag t = tagRepository.findById(id);
        if (t == null){
            throw new NotFoundException("不存在该标签");
        }else {
            tagRepository.update(id,name);
        }

    }

    @Transactional
    @Override
    public void deleteTag(Integer id) {
        tagRepository.delete(id);
    }

    @Override
    public Integer getCount() {
        return tagRepository.count();
    }
}
